(library (shell-state)
  (export get-shell-state
          update-shell-state*
          update-shell-state
          default-shell-state)
  (import (except (rnrs base) error map)
          (only (guile)
                lambda* λ
                getcwd)
          ;; (ice-9 exceptions)
          ;; lists
          (srfi srfi-1)
          (list-helpers)
          (alist-helpers))


  (define default-shell-state
    (let ([initial-pwd (getcwd)])
      `((last-exit-code . 0)
        (current-working-directory . ,initial-pwd)
        (directory-stack . ,(list initial-pwd)))))


  (define update-shell-state
    (λ (shell-state key val)
      "Data abstraction function for updating the shell state. Shell state
is currently an alist, but maybe that changes in the future."
      (alist-set shell-state key val)))


  (define update-shell-state*
    (λ (shell-state keys vals)
      "Data abstraction function for updating the shell state. Shell state
is currently an alist, but maybe that changes in the future."
      (let iter ([shell-state° shell-state]
                 [keys° keys]
                 [vals° vals])
        (cond
         [(null? keys°) shell-state°]
         [else
          (iter (alist-set shell-state° (car keys°) (car vals°))
                (cdr keys°)
                (cdr vals°))]))))


  (define get-shell-state
    (λ (shell-state key)
      "Data abstraction function for updating the shell state. Shell state
is currently an alist, but maybe that changes in the future."
      (alist-refs shell-state (list key)))))
